/// Author:					Joe Audette
/// Created:				2007-03-01
/// Last Modified:			2009-02-01
/// 
/// The use and distribution terms for this software are covered by the 
/// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)  
/// which can be found in the file CPL.TXT at the root of this distribution.
/// By using this software in any fashion, you are agreeing to be bound by 
/// the terms of this license.
///
/// You must not remove this notice, or any other, from this software.

using System;
using System.Data;
using eStore.Data;

namespace eStore.Business
{
    /// <summary>
    /// Represents the availability of an product
    /// </summary>
    public class ProductAvailability
    {

        #region Constructors

        public ProductAvailability()
        { }


        public ProductAvailability(Guid guid)
        {
            GetProductAvailability(guid);
        }

        #endregion

        #region Private Properties

        private Guid guid = Guid.Empty;
        private Guid productGuid = Guid.Empty;
        private DateTime beginUTC;
        private DateTime endUTC;
        private bool requiresProductCode;
        private string productCode;
        private int maxAllowedPerCustomer;
        private DateTime created = DateTime.UtcNow;
        private Guid createdBy = Guid.Empty;
        private string createdFromIP;
        private bool isDeleted;
        private Guid deletedBy = Guid.Empty;
        private DateTime deletedTime;
        private string deletedFromIP;
        private DateTime lastModified = DateTime.UtcNow;
        private Guid lastModifedBy = Guid.Empty;
        private string lastModifedFromIP;



        #endregion

        #region Public Properties

        public Guid Guid
        {
            get { return guid; }
            set { guid = value; }
        }
        public Guid ProductGuid
        {
            get { return productGuid; }
            set { productGuid = value; }
        }
        public DateTime BeginUtc
        {
            get { return beginUTC; }
            set { beginUTC = value; }
        }
        public DateTime EndUtc
        {
            get { return endUTC; }
            set { endUTC = value; }
        }
        public bool RequiresProductCode
        {
            get { return requiresProductCode; }
            set { requiresProductCode = value; }
        }
        public string ProductCode
        {
            get { return productCode; }
            set { productCode = value; }
        }
        public int MaxAllowedPerCustomer
        {
            get { return maxAllowedPerCustomer; }
            set { maxAllowedPerCustomer = value; }
        }
        public DateTime Created
        {
            get { return created; }
            set { created = value; }
        }
        public Guid CreatedBy
        {
            get { return createdBy; }
            set { createdBy = value; }
        }
        public string CreatedFromIP
        {
            get { return createdFromIP; }
            set { createdFromIP = value; }
        }
        public bool IsDeleted
        {
            get { return isDeleted; }
            set { isDeleted = value; }
        }
        public Guid DeletedBy
        {
            get { return deletedBy; }
            set { deletedBy = value; }
        }
        public DateTime DeletedTime
        {
            get { return deletedTime; }
            set { deletedTime = value; }
        }
        public string DeletedFromIP
        {
            get { return deletedFromIP; }
            set { deletedFromIP = value; }
        }
        public DateTime LastModified
        {
            get { return lastModified; }
            set { lastModified = value; }
        }
        public Guid LastModifedBy
        {
            get { return lastModifedBy; }
            set { lastModifedBy = value; }
        }
        public string LastModifedFromIP
        {
            get { return lastModifedFromIP; }
            set { lastModifedFromIP = value; }
        }

        #endregion

        #region Private Methods



        private void GetProductAvailability(Guid guid)
        {
            using (IDataReader reader = DBProductAvailability.Get(guid))
            {
                if (reader.Read())
                {
                    this.guid = new Guid(reader["Guid"].ToString());
                    this.productGuid = new Guid(reader["ProductGuid"].ToString());
                    this.beginUTC = Convert.ToDateTime(reader["BeginUTC"]);
                    this.endUTC = Convert.ToDateTime(reader["EndUTC"]);
                    this.requiresProductCode = Convert.ToBoolean(reader["RequiresProductCode"]);
                    this.productCode = reader["ProductCode"].ToString();
                    this.maxAllowedPerCustomer = Convert.ToInt32(reader["MaxAllowedPerCustomer"]);
                    this.created = Convert.ToDateTime(reader["Created"]);
                    this.createdBy = new Guid(reader["CreatedBy"].ToString());
                    this.createdFromIP = reader["CreatedFromIP"].ToString();
                    this.isDeleted = Convert.ToBoolean(reader["IsDeleted"]);
                    if (reader["DeletedBy"] != DBNull.Value)
                    {
                        this.deletedBy = new Guid(reader["DeletedBy"].ToString());
                    }
                    if (reader["DeletedTime"] != DBNull.Value)
                    {
                        this.deletedTime = Convert.ToDateTime(reader["DeletedTime"]);
                    }
                    this.deletedFromIP = reader["DeletedFromIP"].ToString();
                    this.lastModified = Convert.ToDateTime(reader["LastModified"]);
                    this.lastModifedBy = new Guid(reader["LastModifedBy"].ToString());
                    this.lastModifedFromIP = reader["LastModifedFromIP"].ToString();

                }

            }

        }

        private bool Create()
        {
            Guid newID = Guid.NewGuid();

            this.guid = newID;

            int rowsAffected = DBProductAvailability.Add(
                this.guid,
                this.productGuid,
                this.beginUTC,
                this.endUTC,
                this.requiresProductCode,
                this.productCode,
                this.maxAllowedPerCustomer,
                this.created,
                this.createdBy,
                this.createdFromIP,
                this.lastModified,
                this.lastModifedBy,
                this.lastModifedFromIP);

            return (rowsAffected > 0);

        }



        private bool Update()
        {
            ProductAvailability availability = new ProductAvailability(this.guid);
            DBProductAvailability.AddHistory(
                Guid.NewGuid(),
                availability.Guid,
                availability.ProductGuid,
                availability.BeginUtc,
                availability.EndUtc,
                availability.RequiresProductCode,
                availability.ProductCode,
                availability.MaxAllowedPerCustomer,
                availability.Created,
                availability.CreatedBy,
                availability.CreatedFromIP,
                availability.IsDeleted,
                availability.DeletedBy,
                availability.DeletedTime,
                availability.DeletedFromIP,
                availability.LastModified,
                availability.LastModifedBy,
                availability.LastModifedFromIP,
                DateTime.UtcNow);

            return DBProductAvailability.Update(
                this.guid,
                this.beginUTC,
                this.endUTC,
                this.requiresProductCode,
                this.productCode,
                this.maxAllowedPerCustomer,
                this.lastModified,
                this.lastModifedBy,
                this.lastModifedFromIP);

        }


        #endregion

        #region Public Methods


        public bool Save()
        {
            if (this.guid != Guid.Empty)
            {
                return Update();
            }
            else
            {
                return Create();
            }
        }




        #endregion

        #region Static Methods

        public static bool Delete(
            Guid guid,
            Guid deletedBy,
            DateTime deletedTime,
            string deletedFromIP)
        {
            ProductAvailability availability = new ProductAvailability(guid);
            DBProductAvailability.AddHistory(
                Guid.NewGuid(),
                availability.Guid,
                availability.ProductGuid,
                availability.BeginUtc,
                availability.EndUtc,
                availability.RequiresProductCode,
                availability.ProductCode,
                availability.MaxAllowedPerCustomer,
                availability.Created,
                availability.CreatedBy,
                availability.CreatedFromIP,
                availability.IsDeleted,
                availability.DeletedBy,
                availability.DeletedTime,
                availability.DeletedFromIP,
                availability.LastModified,
                availability.LastModifedBy,
                availability.LastModifedFromIP,
                DateTime.UtcNow);

            return DBProductAvailability.Delete(
                guid,
                deletedBy,
                deletedTime,
                deletedFromIP);
        }




        public static DataTable GetByProduct(Guid productGuid)
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("Guid", typeof(Guid));
            dataTable.Columns.Add("ProductGuid", typeof(Guid));
            dataTable.Columns.Add("BeginUTC", typeof(DateTime));
            dataTable.Columns.Add("EndUTC", typeof(DateTime));
            dataTable.Columns.Add("RequiresProductCode", typeof(bool));
            dataTable.Columns.Add("ProductCode", typeof(string));
            dataTable.Columns.Add("MaxAllowedPerCustomer", typeof(int));

            using (IDataReader reader = DBProductAvailability.GetByProduct(productGuid))
            {
                while (reader.Read())
                {
                    DataRow row = dataTable.NewRow();
                    row["Guid"] = reader["Guid"];
                    row["ProductGuid"] = reader["ProductGuid"];
                    row["BeginUTC"] = reader["BeginUTC"];
                    row["EndUTC"] = reader["EndUTC"];
                    row["RequiresProductCode"] = reader["RequiresProductCode"];
                    row["ProductCode"] = reader["ProductCode"];
                    row["MaxAllowedPerCustomer"] = reader["MaxAllowedPerCustomer"];
                    dataTable.Rows.Add(row);
                }

            }

            return dataTable;

        }



        #endregion


    }

}
